@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 设置颜色
set "RED=[91m"
set "GREEN=[92m"
set "YELLOW=[93m"
set "BLUE=[94m"
set "NC=[0m"

REM 获取版本号
set "VERSION=%1"
if "%VERSION%"=="" (
    echo %RED%错误: 请指定版本号%NC%
    echo %YELLOW%使用方法: upload-only.bat [版本号] [服务器地址]%NC%
    echo %YELLOW%示例: upload-only.bat v1.0.0 root@192.168.1.100:/opt/deploy%NC%
    pause
    exit /b 1
)

REM 服务器配置
set "SERVER_CONFIG=%2"
if "%SERVER_CONFIG%"=="" (
    echo %RED%错误: 请指定服务器地址%NC%
    echo %YELLOW%使用方法: upload-only.bat [版本号] [服务器地址]%NC%
    echo %YELLOW%示例: upload-only.bat v1.0.0 root@192.168.1.100:/opt/deploy%NC%
    pause
    exit /b 1
)

REM 解析服务器配置
for /f "tokens=1,2 delims=@" %%a in ("%SERVER_CONFIG%") do (
    set "SERVER_USER=%%a"
    set "SERVER_HOST_PATH=%%b"
)
for /f "tokens=1,2 delims=:" %%a in ("%SERVER_HOST_PATH%") do (
    set "SERVER_HOST=%%a"
    set "SERVER_PATH=%%b"
)

echo %GREEN%=== LikeAdmin Python 文件上传脚本 ===%NC%
echo %BLUE%版本号: %VERSION%%NC%
echo %BLUE%服务器: %SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%%NC%

REM 检查SCP是否可用
scp -V >nul 2>&1
if errorlevel 1 (
    echo %RED%错误: SCP 不可用，请安装 Git Bash 或 WSL%NC%
    echo %YELLOW%请手动上传文件: likeadmin-python-%VERSION%.zip%NC%
    pause
    exit /b 1
)

REM 检查压缩包是否存在
if not exist "likeadmin-python-%VERSION%.zip" (
    echo %RED%错误: 压缩包 likeadmin-python-%VERSION%.zip 不存在%NC%
    echo %YELLOW%请先运行 build-images-only.bat 构建镜像%NC%
    pause
    exit /b 1
)

REM 检查服务器连接
echo %YELLOW%检查服务器连接...%NC%
ssh "%SERVER_USER%@%SERVER_HOST%" "echo '连接成功'" >nul 2>&1
if errorlevel 1 (
    echo %RED%错误: 无法连接到服务器%NC%
    echo %YELLOW%请检查网络连接和服务器配置%NC%
    pause
    exit /b 1
)

echo %GREEN%服务器连接正常%NC%

REM 确认上传
echo %YELLOW%确认上传以下文件到服务器？(Y/N)%NC%
echo %BLUE%  - likeadmin-python-%VERSION%.zip%NC%
echo %BLUE%  - ../docker-compose.yml%NC%
echo %BLUE%  - deploy-from-archive.sh%NC%
echo %BLUE%  - ../daemon.json%NC%
echo.
set /p "CONFIRM="
if /i not "%CONFIRM%"=="Y" (
    echo %YELLOW%上传已取消%NC%
    pause
    exit /b 1
)

echo %GREEN%=== 开始上传文件 ===%NC%

REM 上传镜像压缩包
echo %YELLOW%上传镜像压缩包...%NC%
scp "likeadmin-python-%VERSION%.zip" "%SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%/"
if errorlevel 1 (
    echo %RED%镜像压缩包上传失败%NC%
    pause
    exit /b 1
)
echo %GREEN%镜像压缩包上传成功%NC%

REM 上传Docker Compose配置
echo %YELLOW%上传Docker Compose配置...%NC%
scp "../docker-compose.yml" "%SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%/"
if errorlevel 1 (
    echo %RED%Docker Compose配置上传失败%NC%
    pause
    exit /b 1
)
echo %GREEN%Docker Compose配置上传成功%NC%

REM 上传部署脚本
echo %YELLOW%上传部署脚本...%NC%
scp "deploy-from-archive.sh" "%SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%/"
if errorlevel 1 (
    echo %RED%部署脚本上传失败%NC%
    pause
    exit /b 1
)
echo %GREEN%部署脚本上传成功%NC%

REM 上传Docker镜像源配置
echo %YELLOW%上传Docker镜像源配置...%NC%
scp "../daemon.json" "%SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%/"
if errorlevel 1 (
    echo %RED%Docker镜像源配置上传失败%NC%
    pause
    exit /b 1
)
echo %GREEN%Docker镜像源配置上传成功%NC%

echo %GREEN%=== 文件上传完成 ===%NC%

REM 询问是否立即部署
echo %YELLOW%是否立即在服务器上部署？(Y/N)%NC%
set /p "DEPLOY="
if /i "%DEPLOY%"=="Y" (
    echo %GREEN%=== 开始远程部署 ===%NC%
    
    REM 远程执行部署脚本
    ssh "%SERVER_USER%@%SERVER_HOST%" "cd %SERVER_PATH% && chmod +x deploy-from-archive.sh && ./deploy-from-archive.sh %VERSION%"
    
    if errorlevel 1 (
        echo %RED%远程部署失败%NC%
        echo %YELLOW%请手动登录服务器执行部署%NC%
    ) else (
        echo %GREEN%远程部署成功%NC%
    )
) else (
    echo %YELLOW%跳过自动部署%NC%
    echo %BLUE%请手动登录服务器执行: ./deploy-from-archive.sh %VERSION%%NC%
)

echo %GREEN%=== 上传操作完成 ===%NC%
echo %BLUE%版本: %VERSION%%NC%
echo %BLUE%服务器: %SERVER_USER%@%SERVER_HOST%:%SERVER_PATH%%NC%
echo %YELLOW%上传文件: likeadmin-python-%VERSION%.zip%NC%

pause 